from math import atan, pi
import numpy as np
import matplotlib.pyplot as plt


plt.subplot(221)
# 移动坐标轴
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', -1))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', -2))

# 画线
x = np.linspace(-2, 1, 1000)
def f(x): return (x**2-1)/(x-1)


plt.plot(x, f(x), label='removable discontinuity')
# 标点
plt.plot(1, 2-0.15, marker='^', color='red', linewidth=2, markersize=12)
plt.text(1, 2, 'point')
# 规定x轴
# plt.xticks(range(-2, 3, 1), range(-2, 3, 1))
plt.xticks(range(-2, 3, 1))
plt.xlim(-2, 2)
plt.ylim(-1-0.15, 2)
# 图例
plt.legend(loc='center')


plt.subplot(222)

# 移动坐标轴
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', -2))

x = np.linspace(-2, 2, 1000)
plt.plot(x, np.abs(x), label='jump discontinuity')
plt.xticks(range(-2, 3, 1))
plt.xlim(-2, 2)
plt.ylim(0-0.15, 2)
plt.legend(loc='upper center')
plt.plot(0, 0-0.1, marker='^', color='red', linewidth=2, markersize=12)
plt.text(0, 0, 'point')

plt.subplot(223)

# 移动坐标轴
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', -1.0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', -20))

# x = np.linspace(-20, 20, 18)
x = [atan(-1)-pi*7, -pi*7+atan(1.1)]
while x[-1] < 20:
    x.append(x[-2]+pi/2)
plt.plot(x, np.tan(x), label='infinite discontinuity')

# plt.xticks(np.linspace(-20, 20, 5))
plt.xlim(-20, 20)
plt.ylim(-1, 1)
plt.yticks(np.linspace(-1.0, 1.0, 5))
plt.legend(loc='upper center')


plt.subplot(224)


# 移动坐标轴
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', -1.0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', -0.5))

x = np.linspace(-0.5, 0.5, 1000)
plt.plot(x, np.sin(1/x), label='discontinuous point of oscillation')
plt.xlim(-0.5, 0.5)
plt.xticks(np.linspace(-0.4, 0.4, 5))
plt.legend(loc='center')


plt.show()
